package com.atguigu.spzx.product.controller;

import com.atguigu.spzx.model.entity.order.OrderItem;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.h5.ProductSkuDto;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.ProductItemVo;
import com.atguigu.spzx.product.service.ProductService;
import com.atguigu.spzx.product.service.ProductService2;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author syh
 * @create 2023-09-01 10:22
 */
@Tag(name = "商品列表管理")
@RestController
@RequestMapping(value="/api/product")
@SuppressWarnings({"unchecked", "rawtypes"})
public class ProductController {

    @Autowired
    private ProductService2 productService2;

    @Operation(summary = "分页查询")
    @GetMapping(value = "/{page}/{limit}")
    public Result<PageInfo<ProductSku>> findByPage(@Parameter(name = "page", description = "当前页码", required = true) @PathVariable Integer page,
                                                   @Parameter(name = "limit", description = "每页记录数", required = true) @PathVariable Integer limit,
                                                   @Parameter(name = "productSkuDto", description = "搜索条件对象", required = false) ProductSkuDto productSkuDto) {
        PageInfo<ProductSku> pageInfo = productService2.findByPage(page, limit, productSkuDto);
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "商品详情")
    @GetMapping("item/{skuId}")
    public Result<ProductItemVo> item(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable Long skuId) {
        ProductItemVo productItemVo = productService2.item(skuId);
        return Result.build(productItemVo , ResultCodeEnum.SUCCESS);
    }
    @Operation(summary = "获取商品sku信息")
    @GetMapping("getBySkuId/{skuId}")
    public Result<ProductSku> getBySkuId(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable Long skuId) {
        ProductSku productSku = productService2.getBySkuId(skuId);
        return Result.build(productSku , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "根据skuid查询sku数据")
    @GetMapping("/sku/{skuId}")
    public Result findSkuBySkuId(@PathVariable("skuId")Long skuId){
        ProductSku productSku =  productService2.findSkuBySkuId(skuId);
        return Result.build(productSku , ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询订单详情对应的商品列表，同时验库存减库存")
    @PostMapping("/findProductSkus")
    public Result<List<ProductSku>> findProductSkus(@RequestBody List<OrderItem> orderItemList){
        // orderItem 对应 一个sku
        List<ProductSku> productSkus = productService2.findProductSkus(orderItemList);
        return Result.build(productSkus,ResultCodeEnum.SUCCESS);

    }

}
